<template>
  <view class="key-container">
    <!-- 主标题区域 -->
    <view class="header-section">
      <text class="main-title">API 密钥</text>
      <text class="sub-title">请输入您的密钥以开始使用</text>
    </view>

    <!-- 输入区域 -->
    <view class="input-section">
      <view class="input-wrapper">
        <input 
          v-model="apiKey" 
          type="text" 
          placeholder="输入 API 密钥"
          class="key-input"
          :password="!showKey"
        />
        <view class="eye-btn" @click="toggleShowKey">
          <text class="eye-icon">{{ showKey ? '●' : '○' }}</text>
        </view>
      </view>
      
      <view class="input-line" :class="{ active: apiKey }"></view>
    </view>

    <!-- 按钮区域 -->
    <view class="action-section">
      <view class="primary-btn" @click="saveKey" :class="{ active: apiKey }">
        <text class="btn-text">继续</text>
      </view>
      
      <view class="skip-btn" @click="skipSetup">
        <text class="skip-text">跳过</text>
      </view>
    </view>

    <!-- 底部提示 -->
    <view class="footer-section">
      <text class="footer-text">密钥将安全存储在本地设备</text>
    </view>

    <!-- 成功提示 -->
    <view v-if="showSuccess" class="success-overlay">
      <view class="success-content">
        <view class="success-icon">✓</view>
        <text class="success-text">设置完成</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      apiKey: '',
      showKey: false,
      showSuccess: false
    }
  },
  
  onLoad() {
    this.loadSavedKey()
  },
  
  methods: {
    toggleShowKey() {
      this.showKey = !this.showKey
    },
    
    saveKey() {
      if (!this.apiKey.trim()) {
        uni.showToast({
          title: '请输入密钥',
          icon: 'none'
        })
        return
      }
      
      // 保存密钥
      const keyData = {
        key: this.apiKey,
        createTime: new Date().toISOString()
      }
      
      uni.setStorageSync('apiKey', keyData)
      
      // 显示成功提示
      this.showSuccess = true
      
      // 跳转到主应用
      setTimeout(() => {
        this.goToMainApp()
      }, 1500)
    },
    
    skipSetup() {
      this.goToMainApp()
    },
    
    goToMainApp() {
      uni.reLaunch({
        url: '/pages/dashboard/dashboard'
      })
    },
    
    loadSavedKey() {
      const savedKey = uni.getStorageSync('apiKey')
      if (savedKey) {
        this.apiKey = savedKey.key
      }
    }
  }
}
</script>

<style scoped>
.key-container {
  min-height: 100vh;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 60rpx;
}

/* 标题区域 */
.header-section {
  text-align: center;
  margin-bottom: 120rpx;
}

.main-title {
  font-size: 64rpx;
  font-weight: 300;
  color: #1a1a1a;
  display: block;
  margin-bottom: 24rpx;
  letter-spacing: 2rpx;
}

.sub-title {
  font-size: 28rpx;
  color: #666666;
  font-weight: 300;
  display: block;
  line-height: 1.5;
}

/* 输入区域 */
.input-section {
  margin-bottom: 100rpx;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.key-input {
  width: 100%;
  height: 80rpx;
  font-size: 32rpx;
  color: #1a1a1a;
  background: transparent;
  border: none;
  outline: none;
  padding: 0;
  font-weight: 300;
}

.key-input::placeholder {
  color: #cccccc;
  font-weight: 300;
}

.eye-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 20rpx;
}

.eye-icon {
  font-size: 24rpx;
  color: #999999;
  font-weight: bold;
}

.input-line {
  height: 2rpx;
  background: #e5e5e5;
  margin-top: 20rpx;
  transition: all 0.3s ease;
}

.input-line.active {
  background: #1a1a1a;
  height: 3rpx;
}

/* 按钮区域 */
.action-section {
  margin-bottom: 80rpx;
}

.primary-btn {
  width: 100%;
  height: 100rpx;
  background: #f5f5f5;
  border-radius: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40rpx;
  transition: all 0.3s ease;
}

.primary-btn.active {
  background: #1a1a1a;
}

.primary-btn.active .btn-text {
  color: #ffffff;
}

.btn-text {
  font-size: 32rpx;
  color: #999999;
  font-weight: 400;
  transition: all 0.3s ease;
}

.skip-btn {
  text-align: center;
  padding: 20rpx;
}

.skip-text {
  font-size: 28rpx;
  color: #999999;
  font-weight: 300;
}

/* 底部提示 */
.footer-section {
  text-align: center;
  margin-top: auto;
  padding-bottom: 60rpx;
}

.footer-text {
  font-size: 24rpx;
  color: #cccccc;
  font-weight: 300;
}

/* 成功提示 */
.success-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.success-content {
  text-align: center;
}

.success-icon {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background: #1a1a1a;
  color: #ffffff;
  font-size: 60rpx;
  font-weight: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30rpx;
}

.success-text {
  font-size: 32rpx;
  color: #1a1a1a;
  font-weight: 300;
}

/* 响应式适配 */
@media screen and (max-width: 750rpx) {
  .key-container {
    padding: 0 40rpx;
  }
  
  .main-title {
    font-size: 56rpx;
  }
  
  .sub-title {
    font-size: 26rpx;
  }
}

/* 动画效果 */
.primary-btn:active {
  transform: scale(0.98);
}

.skip-btn:active {
  opacity: 0.6;
}

.eye-btn:active {
  opacity: 0.6;
}
</style>